<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\grid\CheckboxColumn;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\CommentsRelation */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '评论列表';
$this->params['breadcrumbs'][] = $this->title;
?>
<style>
    /*#table-Show-comments table  tr td:nth-child(4){white-space: nowrap;  text-overflow: ellipsis;  overflow: hidden;  width: 263px;  display: block;}*/
    #table-Show-comments table  tr td:nth-child(4){width: 500px; word-break: break-all; display: block;}
</style>
<div class="comments-relation-index">

<!--    <h1><?/*= Html::encode($this->title) */?></h1>-->
    <div class="box box-success">
        <div class="box-body">
            <?php echo $this->render('_search', ['model' => $searchModel]); ?>

        </div>
    </div>

    <?php \yii\widgets\Pjax::begin(['id'=>'table-Show-comments']); ?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        //'filterModel' => $searchModel,
        'columns' => [
            [
                'class' => CheckboxColumn::className(),
            ],
            //'id',
            //'comment_id',
            'bestry_uid',
            //'order_id',
            //'goods_id',
            'goods_detail.goods_name',
            //'product_id',
            'comments.content',
            [
                'label'=>'评价星级',
                'value'=> function($data){
                    return \common\models\CommentsRelation::$LEVEL[$data->level];
                }
            ],

            [
                'label'=>'状态',
                'format'=>'raw',
                'value'=> function($data){
                    return Html::dropDownList('status', $data->status, \common\models\CommentsRelation::$STATUS);

                }
            ],
            [
                'label'=>'是否展示',
                'format'=>'raw',
                'value'=> function($data){
                    return Html::dropDownList('is_show', $data->is_show, \common\models\CommentsRelation::$IS_SHOW);

                }
            ],

            'create_time',
            //'comments_reply.content',

            [
                'label'=>'回复',
                'format'=>'raw',
                'value'=> function($data){
                    if(empty($data['comments_reply']['content'])){
                        return Html::button('回复', ['class'=>'btn btn-primary', 'name'=>'toReply'])
                            .Html::textarea('reply_text', '', ['style'=>'width: 180px;height: 50px;resize: none;display:none']).'<br>'
                            .Html::button('提交', ['class'=>'btn btn-info btn-sm','name'=>'reply', 'style'=>'display:none']);
                    }
                    else{
                        return Html::textarea('reply_text', $data['comments_reply']['content'],['style'=>'width: 180px;height: 50px;resize: none;'])
                            .'<br/>'.Html::button('修改',['class'=>'btn btn-info btn-sm','name'=>'reply']) ;
                    }


                }
            ],

            // 'is_try',
            // 'is_reply',
            // 'reply_id',
            // 'is_show',
            // 'status',

            ['class' => 'yii\grid\ActionColumn','template'=>'{update}'],
        ],
    ]); ?>
    <p>
        <?= Html::dropDownList('pBatch', 0, ['0'=>'请选择','1'=>'批量审核不通过', '2'=>'批量精华评论', '3'=>'批量回复评论'])?>
        <?= Html::textarea('pReply','',['style'=>'width: 280px;height: 80px;resize: none;display:none'])?>
        <?= Html::button('确定', ['class' => 'btn btn-success', 'id' => 'handle', 'name' => 'handle']) ?>
    </p>
    <?php \yii\widgets\Pjax::end()?>
</div>
<?php $this->beginBlock('js'); ?>
    <script>

        $("body").on('mouseover', '[class="glyphicon glyphicon-pencil"]', function(){
            $("tbody a").attr('title', '修改');
        });
        
        $("body").on('change','[name="is_show"]',function(){

            if(confirm("是否确认修改？")) {

                var id = $(this).parentsUntil('tbody').eq(1).attr('data-key');
                var is_show = $(this).val();
                //alert(is_show);
                $.ajax({
                    url: '<?=Yii::$app->urlManager->createUrl(Yii::$app->controller->id . '/change-show')?>',
                    type: 'post',
                    data: {id: id, is_show: is_show},
                    dataType: 'json',
                    success: function (e) {
                        if(e.status > 0)
                            alert(e.msg);
                        return false;
                    }
                });
            }
        });


        $("body").on('change','[name="status"]', function(){

            if(confirm("是否确认修改？")) {

                var id = $(this).parentsUntil('tbody').eq(1).attr('data-key');
                var status = $(this).val();

                $.ajax({
                    url: '<?=Yii::$app->urlManager->createUrl(Yii::$app->controller->id . '/change-status')?>',
                    type: 'post',
                    data: {id: id, status: status},
                    dataType: 'json',
                    success: function (e) {
                        if(e.status > 0)
                            alert(e.msg);
                        return false;
                    }
                });
            }
        });

        $("body").on('click', '[name="toReply"]', function(){
            $(this).hide();
            $(this).parentsUntil('tbody').find("[name='reply_text']").show();
            $(this).parentsUntil('tbody').find("[name='reply']").show();
        });


        $("body").on('click','[name="reply"]',function(){
            var _this = this;
            var id = $(this).parentsUntil('tbody').eq(1).attr('data-key');
            var content = $(this).parentsUntil('tbody').find("[name='reply_text']").val();
            if(content == ''){
                return false;
            }
            $.ajax({
                url: '<?=Yii::$app->urlManager->createUrl(Yii::$app->controller->id . '/reply')?>',
                type: 'post',
                data: {id:id,content:content},
                dataType: 'json',
                success: function (e) {
                    alert(e.msg);
                    if(e.status == 0){
                        //location.reload();
                        $(_this).parentsUntil('tbody').find("[name='reply']").text('修改');
                    }
                    return false;
                }
            });

        });

        $("body").on('change','[name="pBatch"]', function(){
            var op = $("[name='pBatch']").val();
            if(op != 3){
                //隐藏批量回复文本框
                $('[name="pReply"]').hide();
            }
            else{
                //显示批量回复文本框
                $('[name="pReply"]').show();
            }

        });

        $("body").on('click', '[name="handle"]',function () {

                var ids = new Array();
                var op = $("[name='pBatch']").val();/* 1、批量不通过，2、批量精华评论，3、批量回复评论 */
                //alert(op);

                $('[name="selection[]"]').each(function (i, item) {
                    if ($(item).is(':checked')) {
                        ids.push($(item).parentsUntil('tbody').last().attr('data-key'))

                    }

                });

                if (ids.length == 0) {
                    alert('请至少选择一项!');
                    return
                }
                if(op == 1 || op == 2) {
                    if (confirm('您确定要执行该操作吗？')) {
                        $.ajax({
                            url: '<?=Yii::$app->urlManager->createUrl(Yii::$app->controller->id . '/batch')?>',
                            type: 'get',
                            data: {ids:ids.join(','),op:op},
                            dataType: 'json',
                            success: function (e) {
                                alert(e.msg);

                                if(e.status == 0){
                                    if(op == 1){//如果操作是：批量不通过
                                        $('[name="selection[]"]:checked').prop("checked", false).closest("tr").find("[name='status']").children("option[value=2]").prop("selected", true);

                                    }
                                    else if(op == 2){//如果操作是：批量精华评论
                                        $('[name="selection[]"]:checked').prop("checked", false).closest("tr").find("[name='status']").children("option[value=1]").prop("selected", true);
                                    }

                                 }

                                return false;
                            }
                        });

                    }
                }else if(op == 3){
                    var pContent = $('[name="pReply"]').val();
                    //alert(pContent);
                    if(pContent == ''){
                        alert('回复内容不能为空');
                        return false;
                    }
                    $.ajax({
                        url: '<?=Yii::$app->urlManager->createUrl(Yii::$app->controller->id . '/p-reply')?>',
                        type: 'post',
                        data: {ids:ids.join(','), content:pContent},
                        dataType: 'json',
                        success: function (e) {
                            alert(e.msg);

                            if(e.status == 0){
                                $('[name="selection[]"]:checked').closest("tr").find("[name='toReply']").hide();
                                $('[name="selection[]"]:checked').closest("tr").find("[name='reply_text']").val(pContent).show();
                                $('[name="selection[]"]:checked').prop("checked", false).closest("tr").find("[name='reply']").text('修改').show();
                                $('[name="pReply"]').hide();
                                $('[name="pBatch"]').val(0);

                            }
                            return false;
                        }
                    });
                    
                }

            });

    </script>

<?php $this->endBlock(); ?>